package com.open.thinks.todo.system.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.open.thinks.todo.system.common.IdGenerator;
import com.open.thinks.todo.system.dao.impl.SQLDao;
import com.open.thinks.todo.system.domain.User;

@Service
public class UserService {

	@Resource
	SQLDao sqlDao;

	public void store(User user) {
		if (user.getUser_id() == null)
			user.setUser_id(IdGenerator.getGenerator(User.class).generator());
		sqlDao.persist(user);
	}

	public void update(User user) {
		sqlDao.merge(user);
	}

	public User getUserById(String id) {
		return sqlDao.load(User.class, id);
	}

	public User getUserByName(String name) {
		List<User> users = sqlDao.query4SingleTable(User.class, "select * from users where user_name=?", name);
		if (users != null && users.size() > 0)
			return users.get(0);

		return null;
	}

	public User validate(User user) {
		User fetch_user = getUserByName(user.getUser_name());
		if (fetch_user != null) {
			if (fetch_user.getUser_password().equals(user.getUser_password()))
				return fetch_user;
		}
		return null;
	}

}
